import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'
import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userid:'',
    username: '',
    company: '',
    icon: ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, company, phone }) => {
      state.username = username
      state.company = company
      state.phone = phone
	  console.log('SET_NAME-----',state.username,state.company,state.phone)
    },
    SET_AVATAR: (state, avatar) => {
      state.icon = avatar
    }
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
		  // console.log("userInfo----",userInfo)
       api.login(userInfo).then(response => {
          if(response.data.code ==200){ 
			  // console.log('response.data.code ==200--->',response)
            const result = response.data.result
            const memberInfo = result.memberInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,memberInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', memberInfo.icon)
            commit('SET_NAME', { username: memberInfo.username,company: memberInfo.company,phone: memberInfo.phone})
            resolve(response)
          }else{
            resolve(response)
          }
        }).catch(error => {
			console.log("catch===>response",response)
          reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if(response.data.code ==200){
const result = response.data.result
            const memberInfo = result.memberInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,memberInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', memberInfo.icon)
            commit('SET_NAME', { username: memberInfo.username,company: memberInfo.company,phone: memberInfo.phone})
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
		// console.log('Logout--------------->')
  //       commit('SET_TOKEN', '')
		// commit('SET_NAME', { username: '',company: '',phone: ''})
  //       uni.removeStorageSync(ACCESS_TOKEN)
		// uni.removeStorageSync(USER_INFO)
        api.logout(logoutToken).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },

  },
  getters:{
    token: state => state.token,
	username: state => {state.username=uni.getStorageSync(USER_INFO).username; return state.username},
	company: state => {state.company=uni.getStorageSync(USER_INFO).company; return state.company},
	icon: state => {state.avatar=uni.getStorageSync(USER_INFO).icon; return state.icon},
	userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  }
})
